import Vue from 'vue'
export default {
	namespaced:true,
	state(){
		return{
			cartList:[
				{
					id: "10090",
					twoId:10089,
					french:'Napoleon',
					price:"218.00",
					name:"白色恋人",
					num:1,  //商品数量
					indx:0,   //标志选中的子商品信息
					ischeck:false,
					img:"https://static.mcake.com/goods/tiantianmeimei/R9002/middle/3.jpg",
					list:[
						{id: 10096, sku: "n0301", ahead: "提前5小时预定", edible: "2-3人食", spec: "1磅", price: "198.00"},
						{id: 10097, sku: "n0302", ahead: "提前5小时预定", edible: "4-7人食", spec: "2磅", price: "298.00"},
						{id: 10098, sku: "n0303", ahead: "提前5小时预定", edible: "8-12人食", spec: "3磅", price: "428.00"},
						{id: 10099, sku: "n0305", ahead: "提前5小时预定", edible: "12-20人食", spec: "5磅", price: "728.00"}
					]
				},
				{
					id: "11290",
					twoId:10089,
					french:'Blanc Amant',
					price:"345.00",
					name:"拿破仑莓恋",
					tid:11,
					ischeck:false,
					tname:"限定",
					num:2,  //商品数量
					indx:0, //标志选中的子商品信息
					img:"https://static.mcake.com/goods/tiantianmeimei/R9002/middle/3.jpg",
					list:[
						{id: 10096, sku: "n0301", ahead: "提前5小时预定", edible: "2-3人食", spec: "1磅", price: "198.00"},
						{id: 10097, sku: "n0302", ahead: "提前5小时预定", edible: "4-7人食", spec: "2磅", price: "298.00"},
						{id: 10098, sku: "n0303", ahead: "提前5小时预定", edible: "8-12人食", spec: "3磅", price: "428.00"},
					]  
				}
			]
		}
	},
	//过滤操作，计算
	getters:{
		allInfo(state){
			let allCheck = true
			let allPrice = 0 //总价
			state.cartList.forEach(item=>{
				if(!item.ischeck){
					allCheck = false
				}
				//遍历
				if(item.ischeck){
					allPrice += item.list[item.indx].price * item.num 
				}
			})
			return {allCheck,allPrice}
		}
	},
	mutations:{
		cartCheckMut(state,idx){
			// 单选
			state.cartList[idx].ischeck = !state.cartList[idx].ischeck
		},
		// 全选, bool为原来全选状态
		cartAllCheckMut(state,bool){
			state.cartList.forEach(item=>{
				item.ischeck = !bool
			})
		},
		cartListCheckMut(state,{cartIdx,dropIdx,num}){
			//子商品下拉处理
			state.cartList[cartIdx].indx = dropIdx
			state.cartList[cartIdx].num = num
		},
		cartAddMut(state,goodObj){  
			// 修改已有商品数量
			let {cartList} = state
			let len = cartList.length
			for(let i=0;i<len;i++){
				let {id,indx} = goodObj
				// 购物车已经有相同商品
				if(cartList[i].id==id&&cartList[i].indx==indx){
					state.cartList[i].num++
					return 
				}
			}
			
			//新增商品
			// 非响应的数据挂载
			// goodObj.ischeck = false
			// goodObj.num = 1
			// goodObj.indx = 0
			// 响应式挂载
			Vue.set(goodObj,'ischeck',true)
			Vue.set(goodObj,'num',1)
			// Vue.set(goodObj,'indx',0)
			state.cartList.push(goodObj)
		}
	}
}